package com.kingdee.jdy.controller;

import com.alibaba.fastjson.JSONObject;
import com.kingdee.jdy.util.HttpUtil;
import java.util.HashMap;
import java.util.Random;

/**
 * 仓库
 */
public class StockController {

    public static long saveStock(HashMap<String, Object> urlParam, HashMap<String, Object> headerParam)
        throws Exception {

        long stockid_id = 0;
        JSONObject jsonStock = StockController.prepareStockParam();
        JSONObject resStock = HttpUtil
            .doPost("http://api.kingdee.com/jdy/basedata/store_save", urlParam, headerParam, jsonStock.toJSONString());

        if (resStock != null) {
            JSONObject data = resStock.getJSONObject("data");
            if (data != null) {
                String success = data.getString("success");
                if (success.equalsIgnoreCase("true")) {
                    stockid_id = data.getJSONArray("successPkIds").getLongValue(0);
                    System.out.println("同步仓库成功！stockid_id=" + stockid_id);
                } else {
                    System.out.println("同步仓库失败！");
                }
            } else {
                System.out.println("同步仓库失败！");
            }
        } else {
            System.out.println("同步仓库失败！");
        }
        return stockid_id;
    }

    public static JSONObject prepareStockParam() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", null);
        jsonObject.put("name", "demo测试仓库" + new Random().nextInt());
        jsonObject.put("number", "demoStock" + new Random().nextInt());
        jsonObject.put("isallowfreight", false);
        jsonObject.put("isallowneg", false);

        return jsonObject;
    }



}
